const http = require('http')
const fs =  require('fs')
const request = require('superagent')
const cheerio = require('cheerio')
const schedule = require('node-schedule');

const sendMail =  require('./maile')


let max = 0
// 抓取网站的ur
let url = 'http://api.chinawkb.com/third/queryCoinTrade'

// 5
let rule = new schedule.RecurrenceRule()
rule.second = 5

schedule.scheduleJob(rule, () => {
    main()
})

// 每天7点半
schedule.scheduleJob('30 30 7 * * *', function() {
    max = 0
})

function main() {
    request.get(url, (err, res)=> {	
        
        let price = res.body['rows'][3]['price']
        // 最高价
        let maxPrice = res.body['rows'][3]['price']
        // 最低价
        let minPrice = res.body['rows'][3]['price']
	    //今日涨跌
	    let change = res.body['rows'][3]['change']
        console.log(price, max)
        if(price > max){
            // 修改最高价为现在价格
            max = price
            // 发送邮件通知
	     console.log('我要发送通知了')
             sendMail(max, maxPrice, minPrice, change)
        }	
	})
} 

